package com.yange.accountbook.popup

import android.content.Context
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.lxj.xpopup.impl.PartShadowPopupView
import com.yange.accountbook.R
import com.yange.accountbook.entity.AccountEntity

class ChoiceAccountPop(context: Context) : PartShadowPopupView(context) {

    private lateinit var recyclerView: RecyclerView
    private var mAdapter: MyAdapter? = null
    override fun getImplLayoutId() = R.layout.pop_screen_layout
    private var dataList = mutableListOf<AccountEntity>()

    private lateinit var itemClick: OnItemClickListener

    constructor(
        context: Context,
        dataList: MutableList<AccountEntity>,
        itemClick: OnItemClickListener
    ) : this(context) {
        this.dataList = dataList
        this.itemClick = itemClick
    }

    override fun onCreate() {
        super.onCreate()
        recyclerView = findViewById(R.id.pop_recyclerView)
        recyclerView.layoutManager = LinearLayoutManager(context)
        mAdapter = MyAdapter()
        recyclerView.adapter = mAdapter
        mAdapter?.setNewInstance(dataList)
        mAdapter?.setOnItemClickListener { adapter, _, position ->
            dismiss()
            itemClick.itemClick(adapter.data[position] as AccountEntity)

        }

    }


    class MyAdapter :
        BaseQuickAdapter<AccountEntity, BaseViewHolder>(R.layout.item_mydecorate_flag_layout) {
        override fun convert(holder: BaseViewHolder, item: AccountEntity) {
            holder.setText(R.id.tv_flag, item.name)
        }
    }


    interface OnItemClickListener {
        fun itemClick(item: AccountEntity)

    }
}